from flask import current_app, jsonify
from flask import g
from flask import render_template
from flask import request
from flask import session

from info import constants
from info.models import User, News, Category
from info.utils.common import user_login_data
from info.utils.response_code import RET
from . import index_blu
from info import redis_store


@index_blu.route('/news_list')
def news_list():
    cid = request.args.get('cid', 1)
    page = request.args.get('page', 1)
    per_page = request.args.get('per_page', 10)
    try:
        page = int(page)
        cid = int(cid)
        per_page = int(per_page)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.PARAMERR, errmsg="参数")
    filter = [News.status == 0]
    if cid != 1:
        filter.append(News.category_id == cid)
    try:
        paginate = News.query.filter(*filter).order_by(News.create_time.desc()).paginate(page, per_page, False)
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据查询失败')
    news_model_list = paginate.items
    current_page = paginate.page
    total_page = paginate.pages
    news_dict_list = []
    for news in news_model_list:
        news_dict_list.append(news.to_basic_dict())
    data = {
        "current_page": current_page,
        "total_page": total_page,
        "news_dict_list": news_dict_list
    }
    return jsonify(errno=RET.OK, errmsg='成功', data=data)


@index_blu.route('/')
@user_login_data
def index():
    # user_id = session.get('user_id', None)
    # user = None
    # if user_id:
    #     try:
    #         user = User.query.get(user_id)
    #     except Exception as e:
    #         current_app.logger.error(e)
    user = g.user
    news_list = []
    try:
        news_list = News.query.order_by(News.clicks.desc()).limit(constants.CLICK_RANK_MAX_NEWS)
    except Exception as e:
        current_app.logger.error(e)
    news_dict_list = []
    for news in news_list:
        news_dict_list.append(news.to_basic_dict())
    categories = Category.query.all()
    category_list = []
    for category in categories:
        category_list.append(category.to_dict())
    data = {
        'user': user.to_dict() if user else None,
        'news_dict_list': news_dict_list,
        'category_list': category_list
    }
    return render_template('news/index.html', data=data)


@index_blu.route('/favicon.ico')
def favicon():
    return current_app.send_static_file('news/favicon.ico')